<?php

namespace app\admin\controller;

use app\common\BaseHelper;

use think\Request;

class Company extends Base
{

    public function _initialize() {
        parent::_initialize();

        /*$this->searchFields = [
            'name' => [
                'label'     => '单位名称',
                'field'     => 'name',
                'type'      => 'text',
                'disabled'  => false,
                'condition' => 'like'
            ],
        ];*/
    }

    /*
    public function index(){
        $request = $this->request;
        if($request->isAjax()){
            $model = model($this->model);
            $param = $request->param();
            // 每页起始条数
            $start = $param['start'];
            // 每页显示条数
            $length = $param['length'];
            // 排序条件
            //$columns = $param['order'][0]['column'];
            //$order = $param['columns'][$columns]['data'].' '.$param['order'][0]['dir'];

            $where = $this->getFilterWhere($request);

            if($this->defaultWhere){
                $where = array_merge($this->defaultWhere, $where);
            }

            $list = $model->where($where)->limit($start, $length)->select();
            $count = $model->where($where)->count();
            foreach ($list as &$v){
                $common = new Common();
                $v['city'] = $common->getobjName($v['city']);
                $v['area'] = $common->getobjName($v['area']);
                $v['add'] = $v['city'] . '-' . $v['area'];
            }
            $result = [
                'status' => '1',
                'draw' => $param['draw'],
                'data' => $list,
                'recordsFiltered' => $count,
                'recordsTotal' => $count,
            ];

            return json($result);
        }

        return $this->fetch($this->indexView, [
            'pageSize' => ['url' => fullUrl($request->path())],
            'searchFields' => $this->searchFields,
            'param' => $request->param()
        ]);
    }
    */

    public function index(){
        $info = \app\admin\model\Company::get(1);
        $areaMdl = new \app\admin\model\Area();
        $city = BaseHelper::makeOptions(
            $areaMdl,
            ['level' => 1],
            ['id' => 'asc']
        );

        $area = BaseHelper::getCounty($info['city']);
        $town = BaseHelper::getCounty($info['area']);

        $this->assign('city',$city);
        $this->assign('area',$area);
        $this->assign('town',$town);

        return $this->fetch('edit',[
            'info' => $info
        ]);
    }

    public function detail(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('ststus.ERROR_STATUS'),'参数错误','参数错误');
        }
        $info = \app\admin\model\Company::get($param['id']);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('ststus.ERROR_STATUS'),'参数错误','参数错误');
        }
        $info = \app\admin\model\Company::get($param['id']);
        $areaMdl = new \app\admin\model\Area();
        $city = BaseHelper::makeOptions(
            $areaMdl,
            ['level' => 1],
            ['id' => 'asc']
        );

        $area = BaseHelper::getCounty($info['city']);
        $town = BaseHelper::getCounty($info['area']);

        $this->assign('city',$city);
        $this->assign('area',$area);
        $this->assign('town',$town);

        return $this->fetch('',[
            'info' => $info
        ]);
    }

    public function save(Request $request){
        $model = model($this->model);
        $data = $request->param();
        if(isset($data['id']) && $data['id']){
            $data['createDate'] = date('Y-m-d H:i:s', time());
            $lng = explode(',',$data['addressGeo'])[1];
            $lat = explode(',',$data['addressGeo'])[0];
            $res = Convert_BD09_To_GCJ02($lat,$lng);
            $data['lng'] = $res['lng'];
            $data['lat'] = $res['lat'];
            $result = $model->allowField(true)->save($data, ['id' => $data['id']]);

            if($result !== false) {
                return $this->success('保存成功！');
            } else {
                return $this->error($model->getError());
            }
        }
    }

    /**
     * 筛选条件
     */
    public function getFilterWhere($request){
        $param = $request->param();
        $where = [];
        if($param['search']['value']) {
            $filter = json_decode($param['search']['value'],true);

            if(isset($filter['typeId']) && $filter['typeId']){
                $where['typeId'] = $filter['typeId'];
            }
            if(isset($filter['name']) && $filter['name']){
                $where['name'] = ['like', '%'.$filter['name'].'%'];
            }
        }

        return $where;
    }
}